<?php
class FanFou extends Plugin {
        function info() {
                return array(
                        'url'                   => 'http://www.shihui.org/sheng/fanfou-plugin-for-habari',
                        'name'                  => 'FanFou Plugin',
                        'description'   => 'Automaticly adds fanfou snippet to the sidebar of your webpage.',
                        'license'               => 'Apache License 2.0',
                        'author'                => 'Mike Shi',
                        'authorurl'             => 'http://shihui.org/sheng/',
                        'version'               => '0.6'
                );
        }

        public function filter_plugin_config($actions, $plugin_id) {
                if ($plugin_id == $this->plugin_id()) {
                        $actions[] = _t('Configure');
                }
                return $actions;
        }

        public function action_plugin_ui($plugin_id, $action) {
                if ($plugin_id == $this->plugin_id()) {
                        switch ($action) {
                                case _t('Configure'):
                                        $ui = new FormUI(strtolower(get_class($this)));
                                        $fanfou_name = $ui->add('text', 'name', _t('User Name'));
					$fanfou_showtype = $ui->add('select', 'showtype', _t('Show Type'));
					$fanfou_showtype->options = array('Javascript', 'Flash', 'Image');

					$ui->add('static', 'javascript', _t('<br/><b>The Options below is For Javascript</b>'));
                                        $fanfou_msgtype = $ui->add('checkbox', 'msgtype', _t('Only Show Own Messages'));

					$ui->add('static', 'Flash', _t('<br/><b>The Options below is For Flash</b>'));
					$fanfou_flashtitle = $ui->add('text', 'flashtitle', _t('Title'), _t(self::getvar('name'))._t(' On FanFou'));
					$fanfou_flashmsgnum = $ui->add('select', 'flashmsgnum', _t('Message Number For Showing'));
					$fanfou_flashmsgnum->options = range(20, 1, -1);

					$ui->add('static', 'image', _t('<br/><b>The Options below is For Image</b>'));
					$fanfou_imgwidth = $ui->add('text', 'imagewidth', _t('Width(pixel)'), '200');
					$fanfou_imgmsgnum = $ui->add('select', 'imagemsgnum', _t('Message Number For Showing'), 5);
					$fanfou_imgmsgnum->options = range(5, 1, -1);

                                        $ui->on_success(array($this, 'updated_config'));
                                        $ui->out();
                                break;
                        }
                }
        }

        public function updated_config($ui) {
                return true;
        }

        private static function getvar($var) {
                return Options::get('fanfou:'.$var);
        }

	function action_theme_sidebar_bottom() {
	       
		switch (self::getvar('showtype')) {
			case 0:
				$code = <<<ENDAD
<script type="text/javascript">
fanfou_name="FANFOU_NAME";fanfou_count=10;fanfou_timeline="FANFOU_TYPE";</script>
<script type="text/javascript" charset="utf-8" src="http://static.fanfou.com/badge/timeline.js"></script>
ENDAD;
				$type = 'friends';
				if (self::getvar('msgtype')) {
					$type = 'user';
				}
				$replace = array(
					'FANFOU_NAME'    => urlencode(self::getvar('name')),
					'FANFOU_TYPE' => $type
				);
				$code = str_replace(array_keys($replace), array_values($replace), $code);
				echo $code;
			break;

			case 1:
				$code = <<<ENDAD
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="180" height="400"><param name="movie" value="http://b.fanfou.com/multi.swf" /><param name="flashvars" value="user=FANFOU_NAME&amp;title=FANFOU_TITLE;count=FANFOU_NUM" /><param name="quality" value="autohigh" /><param name="wmode" value="transparent" /><embed type="application/x-shockwave-flash" src="http://b.fanfou.com/multi.swf" flashvars="user=FANFOU_NAME&amp;title=FANFOU_TITLE&amp;count=FANFOU_NUM" quality="autohigh" wmode="transparent" width="180" height="400"></embed></object>
ENDAD;
				$replace = array('FANFOU_NAME' => urlencode(self::getvar('name')),
						'FANFOU_TITLE' => urlencode(self::getvar('flashtitle')),
						'FANFOU_NUM' => 20 - self::getvar('flashmsgnum')
					);
				$code = str_replace(array_keys($replace), array_values($replace), $code);
				echo $code;

			break;

			case 2:
				$code = <<<ENDAD
<a href="http://fanfou.com/FANFOU_NAME" target="_blank" title="饭否"><img src="http://b.fanfou.com/u/FANFOU_NAME/mFANFOU_WIDTH_FANFOU_NUM.png" alt="饭否" /></a>
ENDAD;
				$replace = array('FANFOU_NAME' => urlencode(self::getvar('name')),
						 'FANFOU_WIDTH' => self::getvar('imagewidth'),
						 'FANFOU_NUM' => 5 - self::getvar('imagemsgnum'));
				$code = str_replace(array_keys($replace), array_values($replace), $code);
				echo $code;
			break;

			default:
			break;
		}
        }

}
?>
